package com.biter;

import java.util.Calendar;
import java.util.Date;

import com.biter.service.PollingService;
import com.biter.service.PollingUtils;
import com.biter.util.Formater;
import com.biter.util.StringHelper;
import com.biter.util.Util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SettingActivity extends Activity {
    private SharedPreferences setInfo = null;
    private EditText maxValue = null;
    private EditText minValue = null;
    private EditText TimeInterval = null;
    private Context context = null;
    private TextView appinfo = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		context = this;
		boolean showad = Util.checkShowAd(this);
		
		maxValue = (EditText)findViewById(R.id.setMax);
		minValue = (EditText)findViewById(R.id.setMin);
		appinfo = (TextView)findViewById(R.id.appinfo);
		TimeInterval = (EditText)findViewById(R.id.TimeInterval);
		Button saveButton = (Button)findViewById(R.id.setting_save_btn);
		
		setInfo = getSharedPreferences("setInfo", 0);
		maxValue.setText(setInfo.getString("maxValue", ""));
		minValue.setText(setInfo.getString("minValue", ""));
		TimeInterval.setText(setInfo.getString("TimeInterval", ""));
		saveButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Editor editor = setInfo.edit();
				String min = minValue.getText().toString();
				String max = maxValue.getText().toString();
				String timeInterval = TimeInterval.getText().toString();
				editor.putString("maxValue", max);
				editor.putString("minValue", min);
				editor.putString("TimeInterval", timeInterval);
				editor.commit();
				PollingUtils.stopPollingService(context, PollingService.class, PollingService.ACTION);
				
				PollingUtils.startPollingService(context, Integer.parseInt(timeInterval), PollingService.class, PollingService.ACTION);
				
				StringHelper.sendShortMsg(context, "设置上限:" + max + ", 下限:" + min);
			}
			
		});	
		
		maxValue.setEnabled(!showad);
		minValue.setEnabled(!showad);
		TimeInterval.setEnabled(!showad);
		
		if(showad){
			
			appinfo.setText("请购买30天服务即可使用功能并去除广告.支付成功之后,请重新打开应用.");
		
		}else{
			
			Long pay_time = setInfo.getLong("showAdTime", (new Date()).getTime());
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(pay_time);
			appinfo.setText("VIP到期："+Formater.formatDateTime(calendar.getTime()));
			
		}
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_pays:
            Util.show_pay(context);
			break;
		default:
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}
	
	
}
